# Maintainer: Thore Bödecker <foxxx0@archlinux.org>
# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=consul
_commit=5e08e229dbdaed5adf3ca99afe9df247c51507da
pkgver=1.15.2
pkgrel=1
pkgdesc="A tool for service discovery, monitoring and configuration."
arch=('loong64' 'x86_64')
url="https://www.consul.io"
license=('MPL2')
depends=('glibc')
makedepends=('git' 'go' 'gox' 'go-tools')
#makedepends+=('procps-ng' 'zip' 'yarn' 'nodejs-lts-fermium' 'npm')  # makedepds for the UI
source=("git+https://github.com/hashicorp/consul#commit=${_commit}"
        'consul.service'
        'consul.default'
        'consul.sysusers'
        'example.json'
        )
install=consul.install
backup=('etc/default/consul')
sha512sums=('SKIP'
            'c70b9d1556f6c7ecb2e915ab685f289cef0e31198bd2e50c74a0483bbfb387beec67334f539a90adbf68b61b07946e98b300ab8a8e26e53b35f4ab4894adeb04'
            'ec5a800529a297c709fa383c094ecf106351cf0f8ac7b613b972d415d77fe001088902d7ab805e63e78a8e6360323fec1b795db5a4446df1e21b9b4ed31e7079'
            'ef872aedb2bc022a29292b7972a792b22e684c1ccb904a2b2cfec6d8966c28fb19be1452ce060821c419f1b646b236ba2e783175595e4bb6926d164c27a15c87'
            'c4292b8f56ee955ed7385a49843fd90d6434029891b3e1e724cb2fc841514c06e2554a26d3937c114371b18c2168c4e64319eb2cbd726ee8b35870df19089348')

export CGO_LDFLAGS="${LDFLAGS}"
export CGO_CFLAGS="${CFLAGS}"
export CGO_CPPFLAGS="${CPPFLAGS}"
export CGO_CXXFLAGS="${CXXFLAGS}"
# consul is incompatible with -buildmode=pie
export GOFLAGS="-trimpath -mod=readonly -modcacherw"

export GOOS='linux'
export GOARCH='amd64'
export XC_OSARCH='linux/amd64'

prepare() {
  cd "${srcdir}/${pkgname}"

  local filename
  for filename in "${source[@]}"; do
    if [[ "$filename" =~ \.patch$ ]]; then
      patch -p1 -N -l -i "$srcdir/${filename##*/}"
    fi
  done

  mkdir -p build
}

build() {
  cd "${srcdir}/${pkgname}"
  go build -o build './...'

  # resulting UI build can be found in the dist/ subdirectory
  # TODO: decide if we want the UI and then figure out where to put it in the pkgdir
  #cd ui/packages/consul-ui
  #make build
}

check() {
  cd "${srcdir}/${pkgname}"
  # assert version number from commit hash matches pkgver metadata
  ./build/consul --version | head -1 | grep -qF -- "v${pkgver}"
}

package() {
  cd "${srcdir}/${pkgname}"

  install -D -d -m750 -o 208 -g 208 "${pkgdir}/var/lib/consul"
  install -D -d -m750 -o   0 -g 208 "${pkgdir}/etc/consul.d"

  install -D -m644 "${srcdir}/consul.default" "${pkgdir}/etc/default/consul"
  install -D -m644 -o 0 -g 0 "${srcdir}/example.json" "${pkgdir}/usr/share/doc/consul/config.example.json"
  install -Dm755 "build/consul" "${pkgdir}/usr/bin/consul"

  install -Dm644 "${srcdir}/consul.service" "${pkgdir}/usr/lib/systemd/system/consul.service"
  install -Dm644 "${srcdir}/consul.sysusers" "${pkgdir}/usr/lib/sysusers.d/consul.conf"
}

# vim:set ts=2 sw=2 et:
